package com.workstudy.wstudy.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.workstudy.common.utils.DateUtils;
import com.workstudy.wstudy.domain.WstCreditUser;
import com.workstudy.wstudy.domain.WstStudent;
import com.workstudy.wstudy.mapper.WstCreditUserMapper;
import com.workstudy.wstudy.mapper.WstStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.wstudy.mapper.WstCreditMapper;
import com.workstudy.wstudy.domain.WstCredit;
import com.workstudy.wstudy.service.IWstCreditService;
import com.workstudy.common.core.text.Convert;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 信用管理Service业务层处理
 * 
 * @author sun
 * @date 2021-04-04
 */
@Service
public class WstCreditServiceImpl implements IWstCreditService 
{
    @Autowired
    private WstCreditMapper wstCreditMapper;

    @Autowired
    private WstCreditUserMapper wstCreditUserMapper;

    @Autowired
    private WstStudentMapper wstStudentMapper;

    /**
     * 查询信用管理
     * 
     * @param creditId 信用管理ID
     * @return 信用管理
     */
    @Override
    public WstCredit selectWstCreditById(Long creditId)
    {
        return wstCreditMapper.selectWstCreditById(creditId);
    }

    /**
     * 查询信用管理列表
     * 
     * @param wstCredit 信用管理
     * @return 信用管理
     */
    @Override
    public List<WstCredit> selectWstCreditList(WstCredit wstCredit)
    {
        return wstCreditMapper.selectWstCreditList(wstCredit);
    }

    @Override
    public List<WstCreditUser> selectWstCreditUserList(WstCreditUser creditUser) {
        return wstCreditUserMapper.selectWstCreditUserList(creditUser);
    }

    /**
     * 新增信用管理
     * 
     * @param wstCredit 信用管理
     * @return 结果
     */
    @Override
    public int insertWstCredit(WstCredit wstCredit)
    {
        wstCredit.setCreateTime(DateUtils.getNowDate());
        return wstCreditMapper.insertWstCredit(wstCredit);
    }

    @Override
    @Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertWstCreditUser(WstCreditUser creditUser) {
        String[] creditIds = creditUser.getParams().get("scoreTemplates").toString().split(",");
        int num = 0;
        if (creditIds.length==0){
            return 0;
        }else {
            for (String creditId:creditIds){
                WstCreditUser params = new WstCreditUser();
                params.setUserId(creditUser.getUserId());
                params.setCreditId(new Long(creditId));
                params.setIsDelete(0);
                params.setCreateTime(new Date());
                num+=wstCreditUserMapper.insertWstCreditUser(params);
            }
        }
        //总结分数
        this.sumScore(creditUser.getUserId());
        return num;
    }

    /**
     * 修改信用管理
     * 
     * @param wstCredit 信用管理
     * @return 结果
     */
    @Override
    public int updateWstCredit(WstCredit wstCredit)
    {
        wstCredit.setUpdateTime(DateUtils.getNowDate());
        return wstCreditMapper.updateWstCredit(wstCredit);
    }

    /**
     * 删除信用管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWstCreditByIds(String ids)
    {
        return wstCreditMapper.deleteWstCreditByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除信用管理信息
     * 
     * @param creditId 信用管理ID
     * @return 结果
     */
    @Override
    public int deleteWstCreditById(Long creditId)
    {
        return wstCreditMapper.deleteWstCreditById(creditId);
    }

    @Override
    public int minusByRevoke(Long studentId) {
//        拒绝扣分
        WstCredit params = wstCreditMapper.selectWstCreditByCode("401");
        WstCreditUser wstCreditUser = new WstCreditUser(params.getCreditId(),studentId,0);
        wstCreditUserMapper.insertWstCreditUser(wstCreditUser);
        return sumScore(studentId);
    }

    @Override
    public int sumScore(Long studentId) {
        WstCreditUser wstCreditUser = new WstCreditUser();
        wstCreditUser.setUserId(studentId);
        List<WstCreditUser> list = wstCreditUserMapper.selectWstCreditUserList(wstCreditUser);
        double sum = wstCreditMapper.selectWstCreditByCode("101").getDescore();
        for (WstCreditUser w:list){
            WstCredit credit = wstCreditMapper.selectWstCreditById(w.getCreditId());
            sum+=credit.getDescore();
        }
        WstStudent wstStudent = new WstStudent();
        wstStudent.setStudentId(studentId);
        wstStudent.setCredit(sum);
        return wstStudentMapper.updateWstStudent(wstStudent);
    }
}
